//
//  EFQuickQcodeProductView.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/20.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFQuickQcodeProductView.h"
#import "EFSepecialItem.h"

static const CGFloat qcodeWidth = 80;

@interface EFQuickQcodeProductView ()
@property (strong, nonatomic) UIImageView *imageV;
@property (strong, nonatomic) UILabel *titleLable;
@property (strong, nonatomic) UILabel *salePriceLabel;
@property (strong, nonatomic) EFSepecialItem *item;

@property (strong, nonatomic) UIView *countView;
@property (strong, nonatomic) UILabel *countLabel;
@end

@implementation EFQuickQcodeProductView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame Item:(EFSepecialItem *)item{
    self = [super initWithFrame:frame];
    if (self) {
        self.item = item;
        [self initUI];
        [self refreshUI:item];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
}

- (void)initUI{
    [self addSubview:self.imageV];
    [self addSubview:self.titleLable];
    [self addSubview:self.salePriceLabel];
    
    [self addSubview:self.countView];
    [self.countView addSubview:self.countLabel];
    self.countView.hidden = YES;
}

- (void)refreshUI:(EFSepecialItem *)item{
    [self.imageV setImageWithURL:[NSURL URLWithString:self.item.img]];
    self.titleLable.text = self.item.productName;
    self.titleLable.width = self.width;
    [self.titleLable sizeToFit];
    self.titleLable.lineBreakMode = NSLineBreakByTruncatingTail;
    self.titleLable.top = self.imageV.bottom + 7;
    self.titleLable.left = 0;
    
    self.salePriceLabel.text = [NSString stringWithFormat:@"￥%@",self.item.salePrice];
    [self.salePriceLabel sizeToFit];
    self.salePriceLabel.width = self.width;
    self.salePriceLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    self.salePriceLabel.top = self.titleLable.bottom + 8;
    self.salePriceLabel.left = 5;
    
    if (item.qty.integerValue > 1) {
        self.countView.hidden = NO;
        self.countLabel.text = [NSString stringWithFormat:@"%@",item.qty];
    }
}


- (UIImageView *)imageV{
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, qcodeWidth, qcodeWidth)];
        _imageV.backgroundColor = kGrayColor;
    }
    return _imageV;
}

- (UILabel *)titleLable{
    if (!_titleLable) {
        _titleLable = [[UILabel alloc] init];
        _titleLable.textColor = kBlack333;
        _titleLable.font = [UIFont systemFontOfSize:10];
        _titleLable.numberOfLines = 0;
    }
    return _titleLable;
}

- (UILabel *)salePriceLabel{
    if (!_salePriceLabel) {
        _salePriceLabel = [[UILabel alloc] init];
        _salePriceLabel.textColor = kTextGreenColor;
        _salePriceLabel.font = [UIFont systemFontOfSize:10];
    }
    return _salePriceLabel;
}

- (UIView *)countView{
    if (!_countView) {
        _countView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
        _countView.backgroundColor = RGBCOLOR(0x69ba45);
        _countView.layer.cornerRadius = 15/2;
        _countView.clipsToBounds = YES;
    }
    return _countView;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(2.5, 2.5, 10, 10)];
        _countLabel.textColor = [UIColor whiteColor];
        _countLabel.font = [UIFont systemFontOfSize:10];
    }
    return _countLabel;
}


@end
